package com.yaowk.service.mall.clothing.service.impl;

import com.yaowk.api.mall.clothing.model.GoodsClass;
import com.yaowk.api.mall.clothing.service.IGoodsClassService;
import com.yaowk.service.common.BaseRepository;
import com.yaowk.service.common.BaseServiceImpl;
import com.yaowk.service.mall.clothing.common.StatusDict;
import com.yaowk.service.mall.clothing.dao.GoodsClassDao;
import com.yaowk.service.mall.clothing.entity.CmGoodsClass;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class GoodsClassServiceImpl extends BaseServiceImpl<GoodsClass, CmGoodsClass, Integer> implements IGoodsClassService {

    @Autowired
    private GoodsClassDao dao;


    @Override
    public void show(Integer id) {
        CmGoodsClass cmGoodsClass = dao.findOne(id);
        cmGoodsClass.setIsShow(StatusDict.NORMAL);
        dao.save(cmGoodsClass);
    }

    @Override
    public void hidden(Integer id) {
        CmGoodsClass cmGoodsClass = dao.findOne(id);
        cmGoodsClass.setIsShow(StatusDict.CLOSE);
        dao.save(cmGoodsClass);
    }

    @Override
    public GoodsClass save(GoodsClass model) {
        if (model.isNew()) {
            model = simpleSave(model);
        } else {
            CmGoodsClass cmGoodsClass = dao.findOne(model.getId());
            if (isNotEmpty(model.getDescription()))
                cmGoodsClass.setDescription(model.getDescription());
            if (isNotEmpty(model.getImage()))
                cmGoodsClass.setImage(model.getImage());
            if (isNotEmpty(model.getIsPublic()))
                cmGoodsClass.setIsPublic(model.getIsPublic());
            if (isNotEmpty(model.getIsShow()))
                cmGoodsClass.setIsShow(model.getIsShow());
            if (isNotEmpty(model.getName()))
                cmGoodsClass.setName(model.getName());
            if (isNotEmpty(model.getSort()))
                cmGoodsClass.setSort(model.getSort());
            model = entity2model(dao.save(cmGoodsClass));
        }
        return model;
    }

    @Override
    protected BaseRepository getDao() {
        return dao;
    }

    @Override
    protected CmGoodsClass model2Entity(GoodsClass model) {
        CmGoodsClass cmGoodsClass = null;
        if (isNotEmpty(model)) {
            cmGoodsClass = new CmGoodsClass();
            cmGoodsClass.setIsShow(model.getIsShow());
            cmGoodsClass.setDescription(model.getDescription());
            cmGoodsClass.setImage(model.getImage());
            cmGoodsClass.setIsPublic(model.getIsPublic());
            cmGoodsClass.setName(model.getName());
            cmGoodsClass.setSort(model.getSort());
            cmGoodsClass.setId(model.getId());
        }
        return cmGoodsClass;
    }

    @Override
    protected GoodsClass entity2model(CmGoodsClass model) {
        GoodsClass goodsClass = null;
        if (isNotEmpty(model)) {
            goodsClass = new GoodsClass();
            goodsClass.setIsShow(model.getIsShow());
            goodsClass.setDescription(model.getDescription());
            goodsClass.setImage(model.getImage());
            goodsClass.setIsPublic(model.getIsPublic());
            goodsClass.setName(model.getName());
            goodsClass.setSort(model.getSort());
            goodsClass.setId(model.getId());
        }
        return goodsClass;
    }
}
